package com.gaoyi.basemodule.baseAdapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import com.gaoyi.basemodule.basebean.BaseEntity;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public abstract class BaseAdapter<B extends ViewDataBinding, D extends BaseEntity> extends ListAdapter<D, BaseViewHolder<B>> {

    protected final Context mContext;

    public BaseAdapter(Context context) {
        super(new DiffUtil.ItemCallback<>() {
            @Override
            public boolean areItemsTheSame(@NonNull D oldItem, @NonNull D newItem) {
                if (TextUtils.isEmpty(oldItem.getId())) {
                    return Objects.equals(oldItem, newItem);
                }
                return Objects.equals(oldItem.getId(), newItem.getId());
            }

            @Override
            public boolean areContentsTheSame(@NonNull D oldItem, @NonNull D newItem) {
                return Objects.equals(oldItem, newItem);
//                return newItem.isChange();
            }
        });
        this.mContext = context;
    }

    @NonNull
    @Override
    public BaseViewHolder<B> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        B binding = DataBindingUtil.inflate(LayoutInflater.from(mContext),
                getLayoutResId(), parent, false);
        final BaseViewHolder<B> holder = new BaseViewHolder<>(mContext, binding);
        onCreateView(binding, holder);
        holder.itemView.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClickListener(v, holder.getLayoutPosition());
            }
        });
        holder.itemView.setOnLongClickListener(v -> {
            if (onItemLongClickListener != null) {
                onItemLongClickListener.onItemLongClickListener(v, holder.getLayoutPosition());
            }
            return true;
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder<B> holder, int position) {
        if (holder.mBinding != null) {
            if (getItem(position) != null) {
                getItem(position).setChange(false);
            }
            setBindingVariable(holder.mBinding, position);
            holder.mBinding.executePendingBindings();
            onBindItem(holder.mBinding, getItem(position), position);
        }
    }

    protected abstract void setBindingVariable(B binding, int position);

    protected abstract void onBindItem(B binding, D item, int position);

    /**
     * 创建item view，在这里实现初始化事件，比如点击、触摸事件
     */
    protected abstract void onCreateView(B binding, BaseViewHolder<B> holder);

    protected abstract @LayoutRes
    int getLayoutResId();

    protected List<D> getNewList() {
        return new ArrayList<>(getCurrentList());
    }

    public void add(D data) {
        List<D> newList = getNewList();
        newList.add(data);
        submitList(newList);
    }

    public void addAll(List<D> data) {
        List<D> newList = getNewList();
        newList.addAll(data);
        submitList(newList);
    }

    public void delete(int position) {
        List<D> newList = getNewList();
        newList.remove(position);
        submitList(newList);
    }

    public void clear() {
        submitList(new ArrayList<>());
    }

    public D getPositionData(int position) {
        List<D> newList = getNewList();
        return newList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    protected OnItemClickListener onItemClickListener;//单击事件

    public interface OnItemClickListener {
        void onItemClickListener(View v, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    protected OnItemChildrenClickListener onItemChildrenClickListener;

    public interface OnItemChildrenClickListener {
        void onItemChildrenClickListener(View v, int position);
    }

    public void setOnItemChildrenClickListener(OnItemChildrenClickListener onItemChildrenClickListener) {
        this.onItemChildrenClickListener = onItemChildrenClickListener;
    }

    protected OnItemLongClickListener onItemLongClickListener;

    public interface OnItemLongClickListener {
        void onItemLongClickListener(View v, int position);
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }
}
